import java.sql.*;
import java.lang.String;

public class NumberLocation {
    private static String _city;
    private static String _region;
    private static String _simType;
    private static String _number;
    
    private static final String[] testArray = {
        "13824388774",
        "13822474922",
        "18620309747",
        "18274245531",
        "18684533098",
        "18902373222",
        "07962742244",
        "010248242492",
        "0245238933453",
        "+8613428695822",
    };

    public static void main(String args[]) {  
        try{ 
            Class.forName("org.sqlite.JDBC");
            Connection conn = DriverManager.getConnection("jdbc:sqlite:fuck_table.db");
            Statement stat = conn.createStatement();
            for (String num : testArray) {
                query(stat, num);
            }
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } 

    private static void query(Statement stat, String number) {
        ResultSet rset;
        String num = filtNumber(number);
        try{
            System.out.println("quering " + num);
            if(num.length() == 7) {
                rset = stat.executeQuery("select * from number_location where mobile like "  + num + "; ");
            } else {
                rset = stat.executeQuery("select * from number_location where region like '" + num + "'; ");
            }

            if (rset.next()) {
                System.out.print("number = " + number + " ");
                System.out.println("city = " + rset.getString("city"));
            }
            rset.close();
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }

    private static String filtNumber(String num) {
        String number = num;
        // +86....
        if (number.startsWith("+86")) {
            number = number.substring(3);
        }
        
        // 010 020 021 022 023 024 025 027 028
        if (number.startsWith("01") || number.startsWith("02")) {
            return number.substring(0, 3);
        }
        
        // 0755...
        if (number.startsWith("0")) {
            return number.substring(0, 4);
        }
        
        // 138...
        if (number.length() > 7) {
            return number.substring(0, 7);
        }

        // invalid number
        return "";
    }

    public static String getCityFromNumber(String number) {
        number = filtNumber(number); 
        if (number.equals(_number)) {
            return _city;
        }
        _number = number;
        Connection conn = null;
        Statement stmt = null;
        ResultSet rset = null;
        return null;
    }

    public static String getReginFromNumber(String number) {
        if (number.equals(_number)) {
            return _region;
        } 
        return null;
    }
}
